package com.fubang.pathlearning.popview;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.fubang.pathlearning.R;

import java.util.ArrayList;
import java.util.List;

public class PopUpViewActivity extends AppCompatActivity {

    private ImageView mImageView;
    private RelativeLayout mContainer;
    private RecyclerView mListView;
    private LinearLayoutManager mLayoutManager;
    private PopUpView mPop;
    private View mLine;
    private static int clickPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pop_up_view);
        mImageView = (ImageView) findViewById(R.id.image);
        mContainer = (RelativeLayout) findViewById(R.id.container);
        mLine = findViewById(R.id.line);
        final ArrayList<String> list = new ArrayList<>();
        list.add("自动报警");
        list.add("电气火灾");
        list.add("全部");
        clickPosition = list.size() - 1;
        final MyListAdapter adapter = new MyListAdapter(list, this);
        mPop = new PopUpView(PopUpViewActivity.this);
        mLayoutManager = new LinearLayoutManager(PopUpViewActivity.this);
        mListView = mPop.getListView();
        mListView.setLayoutManager(mLayoutManager);
        mListView.setAdapter(adapter);

        mPop.setCallBack(new PopUpView.CallBack() {
            @Override
            public void onPopDismiss() {
                mImageView.setImageResource(R.mipmap.icon_down);
                mPop.dismiss();
            }
        });

        mContainer.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                mImageView.setImageResource(R.mipmap.icon_up);
                mPop.showAsDropDown(mLine);
                adapter.notifyDataSetChanged();
            }
        });

        adapter.setOnItemClickListener(new MyListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                clickPosition = position;
                Toast.makeText(PopUpViewActivity.this, list.get(position), Toast.LENGTH_SHORT).show();
                mPop.dismiss();
            }
        });
    }


    private static class MyListAdapter extends RecyclerView.Adapter<MyListAdapter.MyViewHolder> implements View.OnClickListener {
        private final LayoutInflater layoutInflater;
        private List<String> list;
        private OnItemClickListener mOnItemClickListener = null;

        private MyListAdapter(List<String> list, Context context) {
            this.list = list;
            layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = layoutInflater.inflate(R.layout.item_popup_window, null, false);
            view.setOnClickListener(this);
            return new MyViewHolder(view);
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            if (holder != null) {
                holder.mTextView.setText(list.get(position));
                holder.itemView.setTag(position);

                if (clickPosition == position) {
                    holder.mTextView.setTextColor(Color.parseColor("#ce4f3b"));
                    holder.mImageView.setVisibility(View.VISIBLE);
                } else {
                    holder.mImageView.setVisibility(View.INVISIBLE);
                    holder.mTextView.setTextColor(Color.parseColor("#999999"));
                }

                if (list != null && list.size() != 0 && position == list.size() - 1) {
                    holder.mLine.setVisibility(View.GONE);
                }
            }
        }

        @Override
        public void onClick(View v) {
            if (mOnItemClickListener != null) {
                mOnItemClickListener.onItemClick(v, (int) v.getTag());
            }
        }

        public void setOnItemClickListener(OnItemClickListener listener) {
            this.mOnItemClickListener = listener;
        }

        public interface OnItemClickListener {
            void onItemClick(View view, int position);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {

            View mLine;
            ImageView mImageView;
            TextView mTextView;

            public MyViewHolder(View itemView) {
                super(itemView);
                mImageView = (ImageView) itemView.findViewById(R.id.image);
                mTextView = (TextView) itemView.findViewById(R.id.text);
                mLine = itemView.findViewById(R.id.line);
            }
        }
    }
}



